package com.gdut.simple.view.activity.user;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.gdut.simple.R;
import com.gdut.simple.contract.HistoryContract;
import com.gdut.simple.data.entity.History;
import com.gdut.simple.presenter.user.HistoryPresenter;
import com.gdut.simple.view.activity.WebActivity;
import com.gdut.simple.view.adapter.HistoryAdapter;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.adapter.base.SimpleTouchHelperCallback;
import com.gdut.simple.view.basic.AbsBasicActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HistoryActivity extends AbsBasicActivity implements HistoryContract.View {

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_item_list_view)
    RecyclerView mHistoryListView;
    @BindView(R.id.id_swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private HistoryAdapter mHistoryAdapter;
    private List<History> mHistoryList;
    private HistoryContract.Presenter mHistoryPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);
        new HistoryPresenter(this);
        initToolbar();
        initRefreshLayout();
        initRecyclerView();
    }

    @Override
    protected void loadData() {
        mHistoryPresenter.load();
    }

    private void initToolbar() {
        mToolbar.setTitle("历史记录");
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    private void initRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(this, R.color.colorPrimaryDark),
                ContextCompat.getColor(this, R.color.colorPrimary),
                ContextCompat.getColor(this, R.color.colorPrimaryDark));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHistoryPresenter.load();
            }
        });
    }

    private void initRecyclerView() {
        mHistoryListView.setLayoutManager(new LinearLayoutManager(this));
        mHistoryListView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mHistoryListView.setItemAnimator(new DefaultItemAnimator());
        mHistoryList = new ArrayList<>();
        mHistoryAdapter = new HistoryAdapter(this, mHistoryList);
        mHistoryListView.setAdapter(mHistoryAdapter);
        mHistoryAdapter.setOnItemClickListener(new AbsRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent(HistoryActivity.this, WebActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("type", mHistoryList.get(position).getType());
                bundle.putString("url", mHistoryList.get(position).getUrl());
                bundle.putString("title", mHistoryList.get(position).getTitle());
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public boolean onLongClick(View view, int position) {
                return false;
            }
        });
        mHistoryAdapter.setOnItemRemoveListener(new HistoryAdapter.OnItemRemoveListener<History>() {
            @Override
            public void remove(History history, int position) {
                mHistoryPresenter.delete(history);
            }
        });
        ItemTouchHelper.Callback callback = new SimpleTouchHelperCallback(mHistoryAdapter);
        ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(mHistoryListView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_clear, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_clear:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示");
                builder.setMessage("是否清空历史记录");
                builder.setPositiveButton("清空", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mHistoryPresenter.clear();
                    }
                });
                builder.setNegativeButton("取消", null);
                builder.show();
                break;
        }
        return true;
    }

    @Override
    public List<History> getItemList() {
        return mHistoryList;
    }

    @Override
    public void refreshList() {
        if (mHistoryAdapter != null) {
            mHistoryAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showRefreshProgress() {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideRefreshProgress() {
        if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void setPresenter(@NonNull HistoryContract.Presenter presenter) {
        mHistoryPresenter = presenter;
    }
}
